<template>
  <div class="center">
    <!-- 头部 -->
    <div class="table_header flex flex_bet flex-wrap">
      <div class="table_button">
        <el-button type="primary" icon="el-icon-refresh" size="mini" @click="Refresh"></el-button>
      </div>
      <div class="table_searchs flex">
        <div class="table_search flex">
          <el-input placeholder="搜索" v-model="searchTable" class="input-with-select" size="small">
            <el-select v-model="select" slot="prepend" placeholder="请选择" class="table_select" style="width:105px">
              <el-option label="用户名" value="1"></el-option>
            </el-select>
            <el-button slot="append" icon="el-icon-search" @click="idnameSearch"></el-button>
          </el-input>
        </div>
        <dynamicTable :checkboxVal="checkboxVal" @alter="alterCheckbok" />
      </div>
    </div>
    <el-table stripe style="width: 100%" :data="browserTable" :key="key" border fit highlight-current-row ref="tableDataRef">
      <el-table-column v-if="colData[0].istrue" min-width="130" align="center" prop="username" label="用户名"></el-table-column>
      <el-table-column v-if="colData[1].istrue" min-width="130" align="center" prop="userTotal" label="总使用流量">
        <template slot-scope="scope">{{ `${scope.row.userTotal} GB` }}</template>
      </el-table-column>
      <el-table-column v-if="colData[2].istrue" min-width="250" align="center" prop="userMonth" label="当月使用流量">
        <template slot-scope="scope">{{ `${scope.row.userMonth} GB` }}</template>
      </el-table-column>
      <el-table-column v-if="colData[3].istrue" min-width="250" align="center" prop="month_flow" label="当月限制流量">
        <template slot-scope="scope">{{ `${scope.row.month_flow } GB` }}</template>
      </el-table-column>
      <el-table-column v-if="colData[4].istrue" min-width="140" align="center" prop="create_time" label="创建时间">
        <template slot-scope="scope">{{scope.row.create_time| newDate}}</template>
      </el-table-column>
      <el-table-column min-width="80" align="center" prop="log" label="操作">
        <template slot-scope="scope">
          <el-row>
            <el-button size="mini" type="primary" @click="getqueryIdStatistics(scope.row.id)">编辑</el-button>
          </el-row>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="paging">
      <el-pagination :current-page.sync="listQuery.startSize" background @size-change="handleSizeChange" @current-change="handleCurrentChange" :page-sizes="[10, 50, 100, 200]" layout="total, sizes, prev, pager, next, jumper" :total="total" :page-size="listQuery.pageSize">
      </el-pagination>
    </div>
    <!-- 添加dialog表单 -->
    <el-dialog title="编辑" width="25%" :visible.sync="dialogFormVisible" @close="addFormclose" :close-on-click-modal="false" :close-on-press-escape="false">
      <el-form :model="compileForm" ref="ruleForm" :rules="rules">
        <el-form-item label="限制流量 (GB):" label-width="130px" prop="monthFlow">
          <el-input v-model="compileForm.monthFlow" class="add_select" placeholder="请输入当月限制流量" maxlength="30"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" round @click="dialogFormVisible = false">取 消</el-button>
        <el-button size="mini" round type="primary" @click="geteditStatistics('ruleForm')">编辑</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import dynamicTable from "@/components/DynamicTable";
import objectable from "@/utils/objectable.js";
import { queryStatistics, queryIdStatistics, editStatistics } from "@/api/allocation.js";
export default {
  components: { dynamicTable },
  data() {
    return {
      checkboxVal: ["用户名", "总使用流量", "当月使用流量", "当月限制流量", "创建时间"],
      key: 1,
      colData: [], //动态控制table
      total: 1,
      searchTable: "",
      select: "", //搜索条件
      //归属table
      browserTable: [],
      listQuery: { startSize: 1, pageSize: 10, type: 1, userId: localStorage.getItem("ID"), username: undefined },
      dialogFormVisible: false,
      compileForm: { id: 0, monthFlow: 0 },
      rules: { monthFlow: [{ required: true, message: "请输入当月限制流量", trigger: "blur" }] }
    }
  },
  created() {
    this.colData = objectable(this.checkboxVal, this.checkboxVal); //转为数组对象，好控制table
    this.getqueryStatistics()
  },
  methods: {
    //根据ID查询信息
    getqueryIdStatistics(id) {
      this.compileForm.id = id
      queryIdStatistics({ id: id }).then(res => {
        this.dialogFormVisible = true
        this.compileForm.monthFlow = res.data.month_flow
      }).catch(err => {
        console.log("请求失败222", err);
      })
    },
    //编辑信息
    geteditStatistics(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          let compileForm = JSON.parse(JSON.stringify(this.compileForm))
          editStatistics(compileForm).then(res => {
            this.$message.success(res.msg)
            this.getqueryStatistics()
            this.dialogFormVisible = false
          }).catch(err => { })
        } else {
          return false
        }
      })
    },
    getqueryStatistics() {
      queryStatistics(this.listQuery).then(res => {
        this.browserTable = res.data
        this.total = res.total
      }).catch(err => {
        this.browserTable = []
        this.total = 0
      })
    },
    handleSizeChange(val) {
      this.listQuery.pageSize = val
      this.getqueryStatistics();
    },
    handleCurrentChange(val) {
      this.listQuery.startSize = val
      this.getqueryStatistics();
    },
    Refresh() {
      this.listQuery = { startSize: 1, pageSize: 10, type: 1, userId: localStorage.getItem("ID"), username: undefined }
      this.getqueryStatistics()
    },
    //编辑添加表单关闭事件
    addFormclose() {
      this.$nextTick(() => { this.$refs.ruleForm.resetFields(); })
    },
    //搜索,1:桌面名，2：包名
    idnameSearch() {
      if (!this.select) return this.$message.warning("请先选择查询条件");
      this.listQuery.startSize = 1
      this.listQuery.pageSize = 10
      switch (this.select) {
        case '1':
          this.listQuery.type = 2
          this.listQuery.username = this.searchTable
          this.getqueryStatistics();
          break;
      }
    },
    //下拉动态table子组件
    alterCheckbok(key, conte) {
      this.key = key;
      this.colData.filter(i => {
        if (conte.indexOf(i.title) != -1) {
          i.istrue = false;
          this.$nextTick(() => {
            this.$refs.tableDataRef.doLayout();
          });
        } else {
          i.istrue = true;
          this.$nextTick(() => {
            this.$refs.tableDataRef.doLayout();
          });
        }
      });
    }
  }
}
</script>